<%@ page language="java" contentType="text/html" 
    pageEncoding="UTF-8"  import="java.net.*, java.io.*, javax.xml.transform.*, javax.xml.transform.stream.*"%>
    


  <%

	final int PAGE_SIZE = 25;
  	final String API_KEY = "90b0723f9926ce3";
  	String pageNo = request.getParameter("page");
  	final String BASE_URL = "http://api.powerhousemuseum.com/api/v1/";
  	String input_url = request.getParameter( "url" );
  	String pageTitle = request.getParameter( "url" ).substring(0,5);
  	
  	
  	//String itemsId = request.getParameter( "items_id" );
  	String default_url = "category/xml/?api_key=" + API_KEY;
  	String useUrl;
  	String input_xsl = request.getParameter( "xsl" )+".xsl";
  	String restServer;
  	String default_xsl = "home.xsl";
  	String xslFile;
  	String orderBy;
  	String default_order = "name";
  	String order = request.getParameter( "order" );
  	

  	
	if(input_url == null)
	{
		useUrl = default_url;
		xslFile = default_xsl;
		orderBy = default_order;
		
	}
	else
	{
		useUrl = input_url+"/xml/?api_key=" + API_KEY;
		xslFile = input_xsl;
		orderBy = order;
	}
		
	
	restServer = BASE_URL + useUrl + "&order_by=" +orderBy + "&limit="+ PAGE_SIZE;
	
	Transformer transformer = TransformerFactory .newInstance().newTransformer(new StreamSource(application.getResourceAsStream(xslFile)));
	
	transformer.setParameter("pageSize", PAGE_SIZE);
	//transformer.setParameter("page",pageNo);
	transformer.setParameter("pageUrl",input_url);
	if(pageTitle.equals("theme"))
	{
		transformer.setParameter("title","theme");
	}
	else if(pageTitle.equals("categ"))
		transformer.setParameter("title","category");
	else if(pageTitle.equals("colle"))
		transformer.setParameter("title","collection");
	else
		transformer.setParameter("title","home");
	

	if(pageNo != null)
	{
		restServer += "&start="+((Integer.parseInt(pageNo)-1)*PAGE_SIZE);   
		
		transformer.setParameter("page",pageNo);
		
	}
	
	URL url = new URL(restServer);
	
	transformer.transform(new StreamSource(url.openStream()), new StreamResult(out));

%>